package com.ming.yourdiary.util;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.ming.yourdiary.YourDiaryApplication;
import com.ming.yourdiary.common.AppConstants;
import com.ming.yourdiary.common.DateConstants;
import com.ming.yourdiary.entity.DiaryNote;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;

public class DataBaseUtil {

  public static final String TAG = "DataBaseUtil";
  public static final SQLiteDatabase DATABASE = YourDiaryApplication.sDiaryNoteDateBase;

  //查询单条数据 暂定传入id即可
  public static DiaryNote searchNote(int targetId) {
    if (DATABASE != null) {
      Log.i(TAG, "SQLiteTest query");
      String selection = "id = ?";
      String[] selectionArgs = {String.valueOf(targetId)};
      Cursor cursor = DATABASE.query(AppConstants.TABLE_NAME, null, selection, selectionArgs, null, null, null);
      while (cursor.moveToNext()) {
        String id = cursor.getString(0);
        String title = cursor.getString(1);
        String content = cursor.getString(2);
        String weatherCode = cursor.getString(3);
        String createDateStr = cursor.getString(4);
        DateTime dateTime = new DateTime(createDateStr, DatePattern.NORM_DATETIME_PATTERN);
        Log.i(TAG, "searchNote by targetId, id:" + id +
          " title:" + title +
          " content:" + content +
          " weatherCode:" + weatherCode +
          " createDate:" + createDateStr);
        return new DiaryNote(Integer.parseInt(id), dateTime, Integer.parseInt(weatherCode), title, content);
      }
    }
    return null;
  }

  //查询多条数据 根据条件查询(暂定按年，月份查询分组)
  public static List<DiaryNote> searchNotes(int year, int month) {
    if (DATABASE != null) {
      String selection = "year = ? and month = ? ";
      String[] selectionArgs = {String.valueOf(year), String.valueOf(month)};
      //未传递年份时默认年份为今年
      if (year == 0) {
        selectionArgs = new String[]{String.valueOf(DateTime.now().year()), String.valueOf(month)};
      }
      //未传递月份时默认查询全年的
      if (month == 0) {
        selection = "year = ?";
        selectionArgs = new String[]{String.valueOf(year)};
      }
      List<DiaryNote> notes = new ArrayList<>();
      Cursor cursor = DATABASE.query(AppConstants.TABLE_NAME, null, selection, selectionArgs, null, null, null);
      while (cursor.moveToNext()) {
        String id = cursor.getString(0);
        String title = cursor.getString(1);
        String content = cursor.getString(2);
        String weatherCode = cursor.getString(3);
        String createDateStr = cursor.getString(4);
        DateTime dateTime = new DateTime(createDateStr, DatePattern.NORM_DATETIME_PATTERN);
        DiaryNote note = new DiaryNote(Integer.parseInt(id), dateTime, Integer.parseInt(weatherCode), title, content);
        Log.i(TAG, "searchNotes by year/month:" + year + "/" + month + ": " + note.toString());
        notes.add(note);
      }
      return notes;
    }
    return null;
  }

  //查询所有数据
  public static List<DiaryNote> searchAll() {
    if (DATABASE != null) {
      Log.i(TAG, "SQLiteTest query");
      List<DiaryNote> notes = new ArrayList<>();
      Cursor cursor = DATABASE.query(AppConstants.TABLE_NAME, null, null, null, null, null, null);
      while (cursor.moveToNext()) {
        String id = cursor.getString(0);
        String title = cursor.getString(1);
        String content = cursor.getString(2);
        String weatherCode = cursor.getString(3);
        String createDateStr = cursor.getString(4);
        DateTime dateTime = new DateTime(createDateStr, DatePattern.NORM_DATETIME_FORMAT);
        notes.add(new DiaryNote(Integer.parseInt(id), dateTime, Integer.parseInt(weatherCode), title, content));
        Log.i(TAG,
          "searchAll, id:" + id +
            " title:" + title +
            " content:" + content +
            " weatherCode:" + weatherCode +
            " createDate:" + createDateStr);
      }
      return notes;
    }
    Log.i(TAG, "searchAll no records");
    return null;
  }

  //查询某一年数据并按月份分组
  public static Map<String, List<DiaryNote>> searchOneYear(int year) {
    if (year == 0) {
      year = DateTime.now().year();
    }
    //LinkedHashMap 遍历后返回数据顺序与插入顺序相同
    Map<String, List<DiaryNote>> resMap = new LinkedHashMap<>();
    for (int i = 1; i < 13; i++) {
      List<DiaryNote> notes = searchNotes(year, i);
      if (notes == null || notes.size() == 0) {
        continue;
      }
      resMap.put(DateConstants.months[i-1], notes);
    }
    return resMap;
  }

  //插入单条数据
  public static void insertNote(DiaryNote note) {
    if (note == null) {
      return;
    }
    if (DATABASE != null) {
      ContentValues values = new ContentValues();
      values.put("title", note.title);
      values.put("content", note.content);
      values.put("weatherCode", note.weatherCode);
      values.put("createDate", note.getDateStr());
      values.put("year", note.getYear());
      values.put("month", note.getMonth() + 1);
      DATABASE.insert(AppConstants.TABLE_NAME, null, values);
      Log.i(TAG, "insertNote note:" + values.toString());
    }
  }

  //插入多条数据
  public static void insertNotes(List<DiaryNote> notes) {
    if (notes == null || notes.size() == 0) {
      return;
    }
    for (DiaryNote note : notes) {
      insertNote(note);
    }
  }

  //删除单条数据 暂定根据id
  public static void delete(int targetId) {
    if (DATABASE != null) {
      String whereClause = "id=?";
      String[] whereArgs = {String.valueOf(targetId)};
      DATABASE.delete(AppConstants.TABLE_NAME, whereClause, whereArgs);
      Log.i(TAG, "delete note by targetId:" + targetId);
    }
  }

  //删除所有记录
  public static void clear() {
    if (DATABASE != null) {
      DATABASE.delete(AppConstants.TABLE_NAME, null, null);
      Log.i(TAG, "clear all records");
    }
  }

}
